﻿using Abp.Authorization;
using Abp.Collections.Extensions;
using Abp.Domain.Entities.Auditing;
using FutureApp.Users;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace FutureApp.Navigation
{
    /// <summary>
    /// 菜单
    /// </summary>
    [Table("Menus")]
    public class Menu : FullAuditedEntity<Guid, User>
    {
        /// <summary>
        /// <see cref="Code"/> 属性的最大长度
        /// </summary>
        public const int MaxCodeLength = 32;

        /// <summary>
        /// <see cref="Name"/> 属性的最大长度
        /// </summary>
        public const int MaxNameLength = 32;

        /// <summary>
        /// <see cref="Icon"/> 属性的最大长度
        /// </summary>
        public const int MaxIconLength = 128;

        /// <summary>
        /// <see cref="NavigationUrl"/> 属性的最大长度
        /// </summary>
        public const int MaxNavigationUrlLength = 200;

        /// <summary>
        /// 初始化 <see cref="Menu"/> 的新实例
        /// </summary>
        public Menu()
        {
            this.Id = Guid.NewGuid();
        }

        /// <summary>
        /// 菜单编码
        /// </summary>
        [Required]
        [StringLength(MaxCodeLength)]
        public virtual string Code { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        [Required]
        [StringLength(MaxNameLength)]
        public virtual string Name { get; set; }

        /// <summary>
        /// 菜单图标
        /// </summary>
        [StringLength(MaxIconLength)]
        public virtual string Icon { get; set; }

        /// <summary>
        /// 菜单排序号
        /// </summary>
        public virtual int Order { get; set; }

        /// <summary>
        /// 菜单访问链接
        /// </summary>
        [StringLength(MaxNavigationUrlLength)]
        public virtual string NavigationUrl { get; set; }

        /// <summary>
        /// 需要身份验证
        /// </summary>
        public virtual bool RequiresAuthentication { get; set; }

        /// <summary>
        /// 身份验证权限字符串
        /// </summary>
        [StringLength(PermissionSetting.MaxNameLength)]
        public virtual string RequiredPermission { get; set; }

        /// <summary>
        /// 父菜单Id
        /// </summary>
        public virtual Guid? ParentId { get; set; }

        /// <summary>
        /// 父菜单
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public virtual Menu Parent { get; set; }

        /// <summary>
        /// 子菜单
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public virtual ICollection<Menu> Children { get; set; }

        /// <inheritDoc/>
        public override string ToString()
        {
            return $"{this.Code} - {this.Name}";
        }
    }
}
